package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.Feedback;
import com.ruoyi.recycle.domain.FinanceManagement;
import com.ruoyi.recycle.dto.FinanceManagementDTO;
import com.ruoyi.recycle.dto.IntegralOrderDTO;
import com.ruoyi.recycle.dto.WithdrawDepositStatistics;
import com.ruoyi.recycle.service.ExchangeRecordService;
import com.ruoyi.recycle.service.IFinanceManagementService;
import com.ruoyi.recycle.service.impl.IntegralOrderServiceImpl;
import com.ruoyi.recycle.vo.ExchangeStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 财务管理Controller
 *
 * @author Wubk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/finance")
@Api(tags = "财务管理")
public class FinanceManagementController extends BaseController {
    @Resource
    private IFinanceManagementService financeManagementService;
    @Resource
    private IntegralOrderServiceImpl integralOrderService;
    @Autowired
    private ExchangeRecordService exchangeRecordService;

    /**
     * 查询财务管理列表
     */
    @RequiresPermissions("recycle:finance:list")
    @GetMapping("/list")
    public TableDataInfo list(FinanceManagement financeManagement) {
        startPage();
        List<FinanceManagement> list = financeManagementService.selectFinanceManagementList(financeManagement);
        return getDataTable(list);
    }

    /**
     * 分页查询财务管理列表
     */
    @PostMapping("/financePageQuery")
    @RequiresPermissions("recycle:finance:list")
    @ApiOperation("分页查询财务管理列表")
    public IPage<FinanceManagement> financePageQuery(@RequestBody FinanceManagementDTO queryDTO) throws Exception{
        final FinanceManagement finance = new FinanceManagement();
        BeanUtils.copyProperties(queryDTO, finance);
        final LambdaQueryWrapper<FinanceManagement> wrapper = Wrappers.lambdaQuery(finance);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(FinanceManagement::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(FinanceManagement::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));
        }
        wrapper.orderByDesc(FinanceManagement::getCreatedTime);
        return financeManagementService.page(new Page<FinanceManagement>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 积分提现统计页面
     *
     * @param
     * @return
     */
    @ApiOperation("积分提现统计页面")
    @PostMapping("/integralWithdrawStatistics")
    public AjaxResult integralWithdrawStatistics(@RequestBody FinanceManagementDTO queryDTO) {

        return financeManagementService.integralWithdrawStatistics(queryDTO);
    }


    /**
     * 积分订单统计页面（复刻订单表，需要替换用户名字段以及统计金额数据）
     * @param
     * @return
     */
    @ApiOperation("积分订单统计页面")
    @PostMapping("/statisticsMoney")
    public AjaxResult withdrawDepositStatistics(@RequestBody IntegralOrderDTO queryDTO) {

        return integralOrderService.statisticsMoney(queryDTO);
    }

    @GetMapping("/exchangeStatistics")
    @ApiOperation("数据统计")
    public R<ExchangeStatisticsVO> exchangeStatistics(@RequestParam Date begin,@RequestParam Date end) {
        return R.ok(exchangeRecordService.exchangeStatistics(begin,end));
    }


    /**
     * 导出财务管理列表
     */
    @RequiresPermissions("recycle:finance:export")
    @Log(title = "财务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出财务管理列表")
    public void export(HttpServletResponse response, FinanceManagement financeManagement) {
        List<FinanceManagement> list = financeManagementService.selectFinanceManagementList(financeManagement);
        ExcelUtil<FinanceManagement> util = new ExcelUtil<FinanceManagement>(FinanceManagement.class);
        util.exportExcel(response, list, "财务管理数据");
    }

    /**
     * 获取财务管理详细信息
     */
    @RequiresPermissions("recycle:finance:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取财务管理详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(financeManagementService.selectFinanceManagementById(id));
    }

    /**
     * 新增财务管理
     */
    @RequiresPermissions("recycle:finance:add")
    @Log(title = "财务管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增财务管理")
    public AjaxResult add(@RequestBody FinanceManagement financeManagement) {
        return toAjax(financeManagementService.insertFinanceManagement(financeManagement));
    }

    /**
     * 修改财务管理
     */
    @RequiresPermissions("recycle:finance:edit")
    @Log(title = "财务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改财务管理")
    public AjaxResult edit(@RequestBody FinanceManagement financeManagement) {
        return toAjax(financeManagementService.updateFinanceManagement(financeManagement));
    }

    /**
     * 删除财务管理
     */
    @RequiresPermissions("recycle:finance:remove")
    @Log(title = "财务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("修改财务管理")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(financeManagementService.deleteFinanceManagementByIds(ids));
    }
}
